<script setup>

</script>

<template>
  <el-card>
    <!--条件查询开始-->
    <el-form ref="queryParams" :model="queryParams" :inline="true" label-width="68px">
      <el-form-item label="所属部门" prop="deptId">
        <el-select
            v-model="queryParams.data.deptId"
            placeholder="请选择部门"
            clearable
            size="small"
            style="width:240px">
          <el-option
              v-for="dict in userTableList"
              :key="dict.deptId"
              :label="dict.deptName"
              :value="dict.deptId"/>
        </el-select>
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select
            v-model="queryParams.data.status"
            placeholder="可用状态"
            clearable
            size="small"
            style="width:240px">
          <el-option
              v-for="dict in statusSelect"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"/>
        </el-select>
      </el-form-item>
      <el-form-item label="创建时间">
        <el-date-picker
            v-model="queryParams.data.dateRange"
            size="small"
            style="width:240px"
            value-format="yyyy-MM-dd"
            type="daterange"
            range-separator="-"
            start-placeholde="开始日期"
            end-placeholde="结束日期"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button type="primary" icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <!-- 表格工具按钮开始 -->
    <el-row :gutter="10" style="margin-bottom: 8px;">
      <el-col :span="1.5">
        <el-button type="success" icon="el-icon-plus" size="mini" @click="handleAdd">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" icon="el-icon-delete" size="mini" @click="batchDelete" :disabled="multiple">
          批量删除
        </el-button>
      </el-col>
    </el-row>
    <!-- 数据表格开始 -->
    <el-table border :data="userTableList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="科室ID" align="center" prop="deptId"/>
      <el-table-column label="科室名称"  align="center" prop="deptName"/>
      <el-table-column label="科室编码" width="130px" align="center" prop="deptNumber"/>
      <el-table-column label="当前挂号量" width="100px" align="center" prop="regcount"/>
      <el-table-column label="排序码" align="center" prop="orderNum"/>
      <el-table-column label="负责人" align="center" prop="deptLeader"/>
      <el-table-column label="电话" align="center" prop="leaderPhone" width="150"/>
      <el-table-column label="状态" prop="statusName" align="center"/>
      <el-table-column label="创建时间" align="center" prop="createTime" width="200"/>
      <el-table-column label="操作" align="center" width="170">
        <template slot-scope="scope">
          <el-button type="text" icon="el-icon-edit" size="mini" @click="handleUpdate(scope.row)">修改
          </el-button>
          <el-button type="text" icon="el-icon-delete" size="mini" @click="handleDelete(scope.row)" :disabled="single">删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-pagination
        v-show="queryParams.total>0"
        :current-page="queryParams.pageNo"
        :page-sizes="[5, 10, 20, 30]"
        :page-size="queryParams.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="queryParams.total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
    />


    <el-dialog
        :title="title"
        :visible.sync="open"
        width="500px"
        center
        append-to-body
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item v-if="title === '修改科室'" label="科室Id" prop="deptId">
          <el-input v-model="form.deptId" placeholder="请输入科室id" clearable size="small" disabled="isEdit"/>
        </el-form-item>
        <el-form-item label="科室名称" prop="deptName">
          <el-input v-model="form.deptName" placeholder="请输入科室名称" clearable size="small"/>
        </el-form-item>
        <el-form-item label="科室编码" prop="deptNumber">
          <el-input v-model="form.deptNumber" placeholder="请输入科室编码" clearable size="small"/>
        </el-form-item>
        <el-form-item label="负责人" prop="deptLeader">
          <el-input v-model="form.deptLeader" placeholder="请输入负责人" clearable size="small"/>
        </el-form-item>
        <el-form-item label="电话" prop="leaderPhone">
          <el-input v-model="form.leaderPhone" placeholder="请输入负责人电话" clearable size="small"/>
        </el-form-item>
        <el-form-item label="排序码" prop="orderNum">
          <el-input-number v-model="form.orderNum" clearable size="small" :min="0"/>
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-radio-group v-model="form.status">
            <el-radio
                v-for="dict in statusSelect"
                :key="dict.dictValue"
                :label="dict.dictValue"
                :value="dict.dictValue"
            >{{ dict.dictLabel }}
            </el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="handleSubmit()">确 定</el-button>
        <el-button @click="cancel()">取 消</el-button>
      </span>
    </el-dialog>

  </el-card>



</template>
<script>

export default {
  data() {
    return {
      queryParams: {
        data: {},
        pageNo: 1,
        pageSize: 5,
        total:0,
      },
      //非单个禁用
      single: false,
      // 非多个禁用
      multiple: true,
      ids: [],
      //排序码默认值
      isEdit: false,
      // 科室列表
      deptOptions: [],
      // 状态列表
      statusOptions: [],
      //用户表格
      userTableList: [],
      //状态字典
      statusSelect: [],
      open: false,
      title: "",
      form: {},
      rules: {
        deptId: [
          {required: true, message: '科室名Id不能为空', trigger: 'blur'}
        ],
        deptName: [
          {required: true, message: '科室名称不能为空', trigger: 'blur'}
        ],
        deptNumber: [
          {required: true, message: '科室编码不能为空', trigger: 'blur'}
        ],
        deptLeader: [
          {required: true, message: '负责人不能为空', trigger: 'blur'}
        ],
        leaderPhone: [
          {required: true, message: '电话不能为空', trigger: 'blur'},
          {required: true, message: '电话格式不正确', trigger: 'blur',pattern:/^1[34578]\d{9}$/}
        ],
        orderNum: [
          {required: true, message: '排序码不能为空', trigger: 'blur'}
        ],
        status: [
          {required: true, message: '状态不能为空', trigger: 'blur'}
        ]
      }
    }
  },
  created() {
    this.formdata();
    this.loadStatus();
    this.statusFormat();
  },
  methods: {
    //选择批量删除
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.deptId)
      this.single = selection.length > 1
      this.multiple = !selection.length

    },

//批量删除
    batchDelete() {
      this.$confirm("是否删除选中数据?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        this.$http.delete("/dept/deleteByIds/" ,this.ids).then(res => {
          if (res.data.code === 200) {
            this.$message.success("删除成功");
            this.formdata();
          }
        })
      })
    },


//修改取消
    cancel() {
      this.open = false;
      this.form = {}
      this.$refs.form.resetFields()
    },
//添加表单
    handleAdd() {
      this.title = "添加科室"
      this.$nextTick(() => {
        this.$refs.form.resetFields()
      })
      this.open = true
      this.form = {
        deptId: "",
        deptName: "",
        deptNumber: "",
        deptLeader: "",
        leaderPhone: "",
        orderNum: "",
        status: ""
      }
    },

//添加修改提交
    handleSubmit() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.$http.post("/dept/upa", this.form).then(res => {
            if (res.data.code === 200) {
              this.$message.success("操作成功");
              this.form = {}
              this.open = false;
              this.formdata();
              this.$refs.form.resetFields()
            } else {
              this.$message.error("操作失败");
            }
          })
        }
      })
    },
//修改
    handleUpdate(row) {
      this.title = "修改科室"
      this.isEdit = this.title === '修改科室';
      this.open = true
      this.$nextTick(() => {
        this.$refs.form.resetFields()
      })
      this.$http.get("/dept/selectDept/" + row.deptId).then(res => {
        this.form = res.data.data
      })
      /*this.form ={
        ...row
      }*/

    },
//逻辑删除
    handleDelete(row) {
      this.$confirm("确认删除选中的项吗？", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        this.$http.delete("/dept/deleteById?deptId=" + row.deptId).then(resp => {
          if (resp.data.code === 200) {
            this.$message.success("删除成功");
            this.formdata()
          }
        }).catch(error =>{
          this.$message.warning("删除失败");
        })
      }).catch(error => {
        this.$message.warning("取消删除");
      })
    },
    //分页pageNo变化时触发
    handleQuery() {
      this.formdata()
    }
    ,
    //重置
    resetQuery() {
      this.queryParams = {
        pageNo: 1,
        pageSize: 5,
        total:0,
        data:{},
      }
      this.formdata()
      // location.reload() //页面刷新
    },
    //加载表单数据
    formdata() {
      this.$http.post('/dept/getAll', this.queryParams).then(res => {
        this.userTableList = res.data.data.list
        this.queryParams.total = res.data.data.total
      })
    },
    //加载状态字典
    loadStatus() {
      this.$http.get('/dept/getStatus').then(res => {
        this.statusSelect = res.data.data
      })
    },
   //格式化状态
    statusFormat(row) {
      let dictLabel = "未知";
      this.statusSelect.filter(item => {
        if (item.dictValue === row.status) {
          dictLabel = item.dictLabel
        }
      })
      return dictLabel;
    },
// 分页pageSize变化时触发
    handleSizeChange(val) {
      this.queryParams.pageSize = val
      this.formdata()
    },
// 点击上一页  下一页，跳转到哪一页面时触发
    handleCurrentChange(val) {
      this.queryParams.pageNo = val
      this.formdata()
    },
  }
  }


</script>

<style scoped>

</style>